import { constantRoutes, asyncRoutes} from '@/router/index'

//当前用户的路由信息
const state = {
    routes:constantRoutes
}
const mutations = {
    //设置动态路由
    SET_ROUTES(state,payload){
        state.routes = [...constantRoutes,...payload]
    }
}
const actions = {
    //获取当前用户拥有的动态路由，调用mutations中的SET_ROUTES
    set_routes(context,payload){
        //创建一个新路由
        const newRoutes = []
        //遍历动态路由进行匹配筛选
        asyncRoutes.forEach(item=>{
            if(payload.includes(item.path.split('/')[1])){
                newRoutes.push(item)
            }
        })
        context.commit('SET_ROUTES',newRoutes)
        return newRoutes
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
  }
  